<?php 
session_start();
ob_start();
?>
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // Always modified
header("Cache-Control: private, no-store, no-cache, must-revalidate"); // HTTP/1.1 
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
?>
<?php include ("ewconfig.php") ?>
<?php include ("db.php") ?>
<?php include ("menuinfo.php") ?>
<?php include ("advsecu.php") ?>
<?php include ("phpmkrfn.php") ?>
<?php include ("ewupload.php") ?>
<?php

// Initialize common variables
$x_id = NULL;
$ox_id = NULL;
$z_id = NULL;
$ar_x_id = NULL;
$ari_x_id = NULL;
$x_idList = NULL;
$x_idChk = NULL;
$cbo_x_id_js = NULL;
$x_status = NULL;
$ox_status = NULL;
$z_status = NULL;
$ar_x_status = NULL;
$ari_x_status = NULL;
$x_statusList = NULL;
$x_statusChk = NULL;
$cbo_x_status_js = NULL;
$x_parent_id = NULL;
$ox_parent_id = NULL;
$z_parent_id = NULL;
$ar_x_parent_id = NULL;
$ari_x_parent_id = NULL;
$x_parent_idList = NULL;
$x_parent_idChk = NULL;
$cbo_x_parent_id_js = NULL;
$x_title = NULL;
$ox_title = NULL;
$z_title = NULL;
$ar_x_title = NULL;
$ari_x_title = NULL;
$x_titleList = NULL;
$x_titleChk = NULL;
$cbo_x_title_js = NULL;
$x_link = NULL;
$ox_link = NULL;
$z_link = NULL;
$ar_x_link = NULL;
$ari_x_link = NULL;
$x_linkList = NULL;
$x_linkChk = NULL;
$cbo_x_link_js = NULL;
$x_type = NULL;
$ox_type = NULL;
$z_type = NULL;
$ar_x_type = NULL;
$ari_x_type = NULL;
$x_typeList = NULL;
$x_typeChk = NULL;
$cbo_x_type_js = NULL;
$x_slug = NULL;
$ox_slug = NULL;
$z_slug = NULL;
$ar_x_slug = NULL;
$ari_x_slug = NULL;
$x_slugList = NULL;
$x_slugChk = NULL;
$cbo_x_slug_js = NULL;
$x_sort = NULL;
$ox_sort = NULL;
$z_sort = NULL;
$ar_x_sort = NULL;
$ari_x_sort = NULL;
$x_sortList = NULL;
$x_sortChk = NULL;
$cbo_x_sort_js = NULL;
$x_created_at = NULL;
$ox_created_at = NULL;
$z_created_at = NULL;
$ar_x_created_at = NULL;
$ari_x_created_at = NULL;
$x_created_atList = NULL;
$x_created_atChk = NULL;
$cbo_x_created_at_js = NULL;
$x_updated_at = NULL;
$ox_updated_at = NULL;
$z_updated_at = NULL;
$ar_x_updated_at = NULL;
$ari_x_updated_at = NULL;
$x_updated_atList = NULL;
$x_updated_atChk = NULL;
$cbo_x_updated_at_js = NULL;
$x_author_id = NULL;
$ox_author_id = NULL;
$z_author_id = NULL;
$ar_x_author_id = NULL;
$ari_x_author_id = NULL;
$x_author_idList = NULL;
$x_author_idChk = NULL;
$cbo_x_author_id_js = NULL;
$x_modified_id = NULL;
$ox_modified_id = NULL;
$z_modified_id = NULL;
$ar_x_modified_id = NULL;
$ari_x_modified_id = NULL;
$x_modified_idList = NULL;
$x_modified_idChk = NULL;
$cbo_x_modified_id_js = NULL;
?>
<?php

// Load key from QueryString
$x_id = @$_GET["id"];

// Get action
$sAction = @$_POST["a_edit"];
if ($sAction == "") {
	$sAction = "I";	// Display record	
} else {

	// Get fields from form
	$x_id = @$_POST["x_id"];
	$x_status = @$_POST["x_status"];
	$x_parent_id = @$_POST["x_parent_id"];
	$x_title = @$_POST["x_title"];
	$x_link = @$_POST["x_link"];
	$x_type = @$_POST["x_type"];
	$x_slug = @$_POST["x_slug"];
	$x_sort = @$_POST["x_sort"];
	$x_created_at = @$_POST["x_created_at"];
	$x_updated_at = @$_POST["x_updated_at"];
	$x_author_id = @$_POST["x_author_id"];
	$x_modified_id = @$_POST["x_modified_id"];
}
if (($x_id == "") || (is_null($x_id))) {
	ob_end_clean();
	switch($_SESSION["session_cms_mode"]){
		case "edit":
			header("Location: menuview.php?id=". $_SESSION["session_cms_id"] . "&mode=" . $_SESSION["session_cms_mode"]);
		break;
		default:
			header("Location: menulist.php");
		break;
	}		
	exit();
}
$conn = phpmkr_db_connect(HOST, USER, PASS, DB, PORT);
switch ($sAction) {
	case "I": // Display record
		if (!LoadData($conn)) { // Load record
			$_SESSION[ewSessionMessage] = "No records found";
			phpmkr_db_close($conn);
			ob_end_clean();
	switch($_SESSION["session_cms_mode"]){
		case "edit":
			header("Location: menuview.php?id=". $_SESSION["session_cms_id"] . "&mode=" . $_SESSION["session_cms_mode"]);
		break;
		default:
			header("Location: menulist.php");
		break;
	}	
			exit();
		}
		break;
	case "U": // Update
		if (EditData($conn)) { // Update record
			$_SESSION[ewSessionMessage] = "Update Record Successful";
			phpmkr_db_close($conn);
			ob_end_clean();
			switch($_SESSION["session_cms_mode"]){
				case "edit":
					header("Location: menuview.php?id=". $_SESSION["session_cms_id"] . "&mode=" . $_SESSION["session_cms_mode"]);
				break;
				default:
					header("Location: menulist.php");
				break;
			}	
			exit();
		}
		break;
}
?>
<?php include ("header.php") ?>
<script type="text/javascript">
<!--
EW_LookupFn = "ewlookup.php"; // ewlookup file name
EW_AddOptFn = "ewaddopt.php"; // ewaddopt.php file name

//-->
</script>
<script type="text/javascript" src="ewp.js"></script>
<script type="text/javascript">
<!--
EW_dateSep = "-"; // set date separator
EW_UploadAllowedFileExt = "gif,jpg,jpeg,bmp,png,swf,flv,zip,mp4"; // allowed upload file extension

//-->
</script>
<script type="text/javascript">
<!--
function EW_checkMyForm(EW_this) {
if (EW_this.x_status && !EW_hasValue(EW_this.x_status, "RADIO")) {
	if (!EW_onError(EW_this, EW_this.x_status, "RADIO", "Please enter required field - status"))
		return false;
}
if (EW_this.x_parent_id && !EW_hasValue(EW_this.x_parent_id, "SELECT")) {
	if (!EW_onError(EW_this, EW_this.x_parent_id, "SELECT", "Please enter required field - parent id"))
		return false;
}
if (EW_this.x_title && !EW_hasValue(EW_this.x_title, "TEXT")) {
	if (!EW_onError(EW_this, EW_this.x_title, "TEXT", "Please enter required field - title"))
		return false;
}
return true;
}

//-->
</script>
<script type="text/javascript">
<!--
	var EW_DHTMLEditors = [];

//-->
</script>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td class="admin_td_title">
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
		  <tr>
			<td class="admin_title"><?=str_replace(","," &gt; ",$_SESSION['session_cms_title'])?> &raquo; Edit &nbsp;</td>
		   </tr>
		 </table>
 	</td>
  </tr>
</table>
<?php
	switch($_SESSION["session_cms_mode"]){
		case "edit":
			echo "<br><a href='menuview.php'>View</a><br />";
		break;
		default:
			echo "<br><a href='menulist.php'>Back to List</a><br />";
		break;
	}
?>
<form name="fmenuedit" id="fmenuedit" action="menuedit.php" method="post" onSubmit="return EW_checkMyForm(this);">
<p>
	<input type="hidden" name="a_edit" value="U">
<?php
if (@$_SESSION[ewSessionMessage] <> "") {
?>
<p><span class="ewmsg"><?php echo $_SESSION[ewSessionMessage]; ?></span></p>
<?php
	$_SESSION[ewSessionMessage] = ""; // Clear message
}
?>
	<table class="ewTable">
<input type="hidden" id="x_id" name="x_id" value="<?php echo @$x_id; ?>">
		<tr>
			<td class="ewTableHeader"><span>status<span class='ewmsg'>&nbsp;*</span></span></td>
			<td class="ewTableAltRow"><span id="cb_x_status">
<?php echo RenderControl(1, 0, 5, 1); ?>
<input type="radio" name="x_status"<?php if (@$x_status == "A") { ?> checked<?php } ?> value="<?php echo htmlspecialchars("A"); ?>">
<?php echo "Aktif"; ?>
<?php echo RenderControl(1, 0, 5, 2); ?>
<?php echo RenderControl(1, 1, 5, 1); ?>
<input type="radio" name="x_status"<?php if (@$x_status == "I") { ?> checked<?php } ?> value="<?php echo htmlspecialchars("I"); ?>">
<?php echo "Tidak Aktif"; ?>
<?php echo RenderControl(1, 1, 5, 2); ?>
</span></td>
		</tr>
		<tr>
			<td class="ewTableHeader"><span>parent id<span class='ewmsg'>&nbsp;*</span></span></td>
			<td class="ewTableAltRow"><span id="cb_x_parent_id">
<?php
$x_parent_idList = "<select id='x_parent_id' name='x_parent_id'>";
$x_parent_idList .= "<option value=''>Please Select</option>";
$sSqlWrk = "SELECT `id`, `title` FROM `menu`";
$sSqlWrk .= " ORDER BY `title` Asc";
$rswrk = phpmkr_query($sSqlWrk,$conn) or die("Failed to execute query at line " . __LINE__ . ": " . phpmkr_error($conn) . '<br>SQL:' . $sSqlWrk);
if ($rswrk) {
	$rowcntwrk = 0;
	while ($datawrk = phpmkr_fetch_array($rswrk)) {
		$x_parent_idList .= "<option value=\"" . htmlspecialchars($datawrk[0]) . "\"";
		if ($datawrk["id"] == @$x_parent_id) {
			$x_parent_idList .= " selected";
		}
		$x_parent_idList .= ">" . $datawrk["title"] . "</option>";
		$rowcntwrk++;
	}
}
@phpmkr_free_result($rswrk);
$x_parent_idList .= "</select>";
echo $x_parent_idList;
?>
</span></td>
		</tr>
		<tr>
			<td class="ewTableHeader"><span>title<span class='ewmsg'>&nbsp;*</span></span></td>
			<td class="ewTableAltRow"><span id="cb_x_title">
<input type="text" name="x_title" id="x_title" size="30" maxlength="255" value="<?php echo htmlspecialchars(@$x_title) ?>">
</span></td>
		</tr>
		<tr>
			<td class="ewTableHeader"><span>link</span></td>
			<td class="ewTableAltRow"><span id="cb_x_link">
<input type="text" name="x_link" id="x_link" size="30" maxlength="255" value="<?php echo htmlspecialchars(@$x_link) ?>">
</span></td>
		</tr>
		<tr>
			<td class="ewTableHeader"><span>type</span></td>
			<td class="ewTableAltRow"><span id="cb_x_type">
<input type="text" name="x_type" id="x_type" size="30" maxlength="255" value="<?php echo htmlspecialchars(@$x_type) ?>">
</span></td>
		</tr>
		<tr>
			<td class="ewTableHeader"><span>slug</span></td>
			<td class="ewTableAltRow"><span id="cb_x_slug">
<input type="text" name="x_slug" id="x_slug" size="30" maxlength="255" value="<?php echo htmlspecialchars(@$x_slug) ?>">
</span></td>
		</tr>
<input type="hidden" id="x_updated_at" name="x_updated_at" value="<?php echo date('Y-m-d h:i:s'); ?>">
<input type="hidden" id="x_modified_id" name="x_modified_id" value="<?php echo $SessionAuthorId; ?>">
	</table>
	<p>
	<input type="submit" name="btnAction" id="btnAction" value="SAVE">
	<input type="button" name="btnCancel" id="btnCancel" value="CANCEL" onclick="javascript:history.back(-1)" />
</form>
<?php include ("footer.php") ?>
<?php
phpmkr_db_close($conn);
?>
<?php

//-------------------------------------------------------------------------------
// Function LoadData
// - Variables setup: field variables

function LoadData($conn)
{
	global $x_id;
	$sFilter = ewSqlKeyWhere;
	if (!is_numeric($x_id)) return false;
	$x_id =  (get_magic_quotes_gpc()) ? stripslashes($x_id) : $x_id;
	$sFilter = str_replace("@id", AdjustSql($x_id), $sFilter); // Replace key value
	$sSql = ewBuildSql(ewSqlSelect, ewSqlWhere, ewSqlGroupBy, ewSqlHaving, ewSqlOrderBy, $sFilter, "");
	$rs = phpmkr_query($sSql,$conn) or die("Failed to execute query at line " . __LINE__ . ": " . phpmkr_error($conn) . '<br>SQL: ' . $sSql);
	if (phpmkr_num_rows($rs) == 0) {
		$bLoadData = false;
	} else {
		$bLoadData = true;
		$row = phpmkr_fetch_array($rs);

		// Get the field contents
		$GLOBALS["x_id"] = $row["id"];
		$GLOBALS["x_status"] = $row["status"];
		$GLOBALS["x_parent_id"] = $row["parent_id"];
		$GLOBALS["x_title"] = $row["title"];
		$GLOBALS["x_link"] = $row["link"];
		$GLOBALS["x_type"] = $row["type"];
		$GLOBALS["x_slug"] = $row["slug"];
		$GLOBALS["x_sort"] = $row["sort"];
		$GLOBALS["x_created_at"] = $row["created_at"];
		$GLOBALS["x_updated_at"] = $row["updated_at"];
		$GLOBALS["x_author_id"] = $row["author_id"];
		$GLOBALS["x_modified_id"] = $row["modified_id"];
	}
	phpmkr_free_result($rs);
	return $bLoadData;
}
?>
<?php

//-------------------------------------------------------------------------------
// Function EditData
// - Variables used: field variables

function EditData($conn)
{
	global $x_id;
	$sFilter = ewSqlKeyWhere;
	if (!is_numeric($x_id)) return false;
	$sTmp =  (get_magic_quotes_gpc()) ? stripslashes($x_id) : $x_id;
	$sFilter = str_replace("@id", AdjustSql($sTmp), $sFilter); // Replace key value
	$sSql = ewBuildSql(ewSqlSelect, ewSqlWhere, ewSqlGroupBy, ewSqlHaving, ewSqlOrderBy, $sFilter, "");
	$rs = phpmkr_query($sSql,$conn) or die("Failed to execute query at line " . __LINE__ . ": " . phpmkr_error($conn) . '<br>SQL: ' . $sSql);

	// Get old recordset
	$oldrs = phpmkr_fetch_array($rs);
	if (phpmkr_num_rows($rs) == 0) {
		return false; // Update Failed
	} else {
		$x_id = @$_POST["x_id"];
		$x_status = @$_POST["x_status"];
		$x_parent_id = @$_POST["x_parent_id"];
		$x_title = @$_POST["x_title"];
		$x_link = @$_POST["x_link"];
		$x_type = @$_POST["x_type"];
		$x_slug = @$_POST["x_slug"];
		$x_updated_at = @$_POST["x_updated_at"];
		$x_modified_id = @$_POST["x_modified_id"];
		$theValue = (!get_magic_quotes_gpc()) ? addslashes($GLOBALS["x_status"]) : $GLOBALS["x_status"]; 
		$theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
		$fieldList["`status`"] = $theValue;
		$theValue = ($GLOBALS["x_parent_id"] != "") ? intval($GLOBALS["x_parent_id"]) : "NULL";
		$fieldList["`parent_id`"] = $theValue;
		$theValue = (!get_magic_quotes_gpc()) ? addslashes($GLOBALS["x_title"]) : $GLOBALS["x_title"]; 
		$theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
		$fieldList["`title`"] = $theValue;
		$theValue = (!get_magic_quotes_gpc()) ? addslashes($GLOBALS["x_link"]) : $GLOBALS["x_link"]; 
		$theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
		$fieldList["`link`"] = $theValue;
		$theValue = (!get_magic_quotes_gpc()) ? addslashes($GLOBALS["x_type"]) : $GLOBALS["x_type"]; 
		$theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
		$fieldList["`type`"] = $theValue;
		$theValue = (!get_magic_quotes_gpc()) ? addslashes($GLOBALS["x_slug"]) : $GLOBALS["x_slug"]; 
		$theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
		$fieldList["`slug`"] = $theValue;
		$theValue = ($GLOBALS["x_updated_at"] != "") ? " '" . ConvertDateToMysqlFormat($GLOBALS["x_updated_at"]) . "'" : "Null";
		$fieldList["`updated_at`"] = $theValue;
		$theValue = ($GLOBALS["x_modified_id"] != "") ? intval($GLOBALS["x_modified_id"]) : "NULL";
		$fieldList["`modified_id`"] = $theValue;

		// Updating event
		if (Recordset_Updating($fieldList, $oldrs)) {

			// Update
			$sSql = "UPDATE `menu` SET ";
			foreach ($fieldList as $key=>$temp) {
				$sSql .= "$key = $temp, ";
			}
			if (substr($sSql, -2) == ", ") {
				$sSql = substr($sSql, 0, strlen($sSql)-2);
			}
			$sSql .= " WHERE " . $sFilter;
			phpmkr_query($sSql,$conn) or die("Failed to execute query at line " . __LINE__ . ": " . phpmkr_error($conn) . '<br>SQL: ' . $sSql);
			$result = (phpmkr_affected_rows($conn) >= 0);

			// Updated event
			if ($result) Recordset_Updated($fieldList, $oldrs);
		} else {
			$result = false; // Update Failed
		}
	}
	return $result;
}

// Updating Event
function Recordset_Updating(&$newrs, $oldrs)
{

	// Enter your customized codes here
	return true;
}

// Updated event
function Recordset_Updated($newrs, $oldrs)
{
	$table = "menu";
}
?>
